package com.goldgov.notice.web;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.goldgov.kcloud.core.json.JsonQueryObject;
import com.goldgov.kcloud.core.json.JsonSuccessObject;
import com.goldgov.login.web.model.LoginAuthUser;
import com.goldgov.notice.service.Notice;
import com.goldgov.notice.service.NoticeQuery;
import com.goldgov.notice.service.NoticeService;
import com.goldgov.notice.service.NoticeUser;
import com.goldgov.notice.service.NoticeUserQuery;
import com.goldgov.notice.service.NoticeUserService;
import com.goldgov.securityworkbench.dict.bean.dto.DictDto;
import com.goldgov.securityworkbench.dict.bean.dto.DictQuery;
import com.goldgov.utils.PropertyUtil;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import springfox.documentation.annotations.ApiIgnore;

/** 
 * @ClassName: NoticeController 
 * @Description: TODO
 * @author MaN
 * @date 2018年7月16日 
 */

@RestController
@RequestMapping("/portal/notice")
@Api("通知公告接口")
public class NoticePortalController {

	@Autowired
	private NoticeService noticeService;
	@Autowired
	private NoticeUserService noticeUserService;
	
	@GetMapping("/listNoticeFull")
	@ApiOperation(value = "通知公告", notes ="")
	@ApiImplicitParams({@ApiImplicitParam(name = "searchType", value = "列表类型", paramType = "query"),
		@ApiImplicitParam(name = "searchPulishBeginDate", value = "发布时间", paramType = "query"),
		@ApiImplicitParam(name = "searchPulishEndDate", value = "发布时间", paramType = "query"),
		@ApiImplicitParam(name = "searchNoticeType", value = "消息类型", paramType = "query")})
	public JsonQueryObject<Notice> listNoticeFull(@ApiIgnore NoticeQuery query,HttpServletRequest request){
		//收件箱
		LoginAuthUser u=LoginAuthUser.getUser(request);
		query.setSearchReceiveUserID(u.getEntityID());
		List<Notice> list = noticeService.getNoticeListFullReceive(query);
		DictQuery queryDict=new DictQuery();
		queryDict.setQueryDictCode("d_noticeType");
		queryDict.setPageSize(-1);
		List<DictDto> listDict=noticeService.findDictListByPage(queryDict);
		for(Notice notice:list) {
			if (PropertyUtil.objectNotEmpty(notice.getNoticeType())) {
				for(DictDto d:listDict) {
						if(d.getDictKey().equals(notice.getNoticeType())) {
							if(PropertyUtil.objectNotEmpty(notice.getNoticeName())) {
								notice.setNoticeName("【"+d.getDictValue()+"】"+notice.getNoticeName());	
							}else {
								notice.setNoticeName("【通知】"+notice.getNoticeName());
							}
							break;
						}
				}
			}else {
				notice.setNoticeName("【通知】"+notice.getNoticeName());
			}
		}
		
		query.setResultList(list);
		return new JsonQueryObject<>(query);
	}
	
	@GetMapping("/getNoticeByID")
	@ApiOperation(value = "根据主键查询会议记录", notes ="工作日志详情")
	public JsonSuccessObject<Notice> getNotice( String id,HttpServletRequest request){
		Notice notice = noticeService.getNoticeById(id);
		LoginAuthUser u=LoginAuthUser.getUser(request);
		DictQuery query=new DictQuery();
		query.setQueryDictCode("d_noticeType");
		query.setPageSize(-1);
		List<DictDto> list=noticeService.findDictListByPage(query);
		if (PropertyUtil.objectNotEmpty(notice.getNoticeType())) {
			for(DictDto d:list) {
					if(d.getDictKey().equals(notice.getNoticeType())) {
						if(PropertyUtil.objectNotEmpty(notice.getNoticeName())) {
							notice.setNoticeName("【"+d.getDictValue()+"】"+notice.getNoticeName());	
						}else {
							notice.setNoticeName("【通知】"+notice.getNoticeName());
						}
						break;
					}
			}
		}else {
			notice.setNoticeName("【通知】"+notice.getNoticeName());
		}
		notice.setReceiveUserID(u.getEntityID());
		notice.setReceiveUserName(u.getName());
		//修改状态已读
		NoticeUser noticeUer=new NoticeUser();
		noticeUer.setNoticeID(id);
		noticeUer.setUserID(u.getEntityID());
		noticeUer.setIsRead(NoticeUserQuery.IS_READ_YES);//已读
		noticeUserService.updateNoticeUser(noticeUer);
		JsonSuccessObject<Notice> returnResult = new JsonSuccessObject<>(notice);
		return returnResult;
	}
	
}
